﻿using UnityEngine;
using System.Collections;
using System;

public class MakeSound : MonoBehaviour {
	public double frequency = 440;
	public double gain = 0.05;

	private double increment;
	private double phase = 0;
	private double sample_frequency = 48000;
	void noise(float[] data, int channels) {
		System.Random rand = new System.Random ();
		for (var i = 0; i  < data.Length; i++) {
			data [i] = (float)(rand.NextDouble () * 2.0 - 1.0);
		}
	}
	void SinSound(float[] data, int channels) {
		increment = frequency * 2 * Math.PI / sample_frequency;
		for (var i = 0; i  < data.Length; i++) {
			phase = phase + increment;
			data[i] = (float)(gain * Math.Sin(phase));
		}
	}
	//440 * 2pi /  48000  步长   
	void PulseSound(float[] data, int channels) {
		increment = frequency * 2 * Math.PI / sample_frequency;
		for (var i = 0; i < data.Length; i++) {
			phase = phase + increment;
			//data[i] = (float)(gain * Math.Sin(phase));

			data[i] = Math.Sign( (float)(Math.Sin(phase)));
		}
	}


	void OnAudioFilterRead(float[] data, int channels) {
		//PulseSound (data, channels);
		noise (data,channels);
	}

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}
